package com.yuji.aacar.controller.splash;

import android.Manifest;
import android.app.AppOpsManager;
import android.content.Intent;
import android.os.Bundle;

import com.yuji.aacar.R;
import com.yuji.aacar.base.BaseActivity;
import com.yuji.aacar.base.UserCenter;
import com.yuji.aacar.controller.login.LoginActivity;
import com.yuji.aacar.controller.main.TabHomeActivity;
import com.yuji.aacar.core.cores.utils.CoreToast;
import com.yuji.aacar.widgets.permission.PermissionsActivity;
import com.yuji.aacar.widgets.permission.PermissionsChecker;

/**
 * <li>Package:com.yuji.aacar.controller.splash</li>
 * <li>Author: lining </li>
 * <li>Date: 2018/2/25</li>
 * <li>Description:  启动页或者闪屏或者中转首页 </li>
 */

public class SplashActivity extends BaseActivity{

    private static final int LOCATION_REQUEST = 0x100;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBarColor(R.color.white);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == LOCATION_REQUEST) {
            if (resultCode == PermissionsActivity.PERMISSIONS_DENIED) {//权限被拒绝
                CoreToast.showToast(this,"读写手机存储权限未打开");
            } else if (resultCode == PermissionsActivity.PERMISSIONS_WAITING) {
                CoreToast.showToast(this,"请到“安全中心-权限管理-AA洗车”，打开读写手机存储权限");
            } else {//权限通过

            }
//            initCrashManager();
//            goNext();
        }
    }

    @Override
    public void alreadyBindBaseViewModel() {
        super.alreadyBindBaseViewModel();

        try {
            PermissionsChecker permissionsChecker = new PermissionsChecker(this);
            if (permissionsChecker.lacksRunTimePermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE) || permissionsChecker.lacksOpsPermissions(AppOpsManager.OPSTR_WRITE_EXTERNAL_STORAGE)) {//权限未通过
                PermissionsActivity.startActivityForResult(this, LOCATION_REQUEST, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, new String[]{AppOpsManager.OPSTR_WRITE_EXTERNAL_STORAGE});
            } else {
            }
        }catch (Exception e){
        }

        if (UserCenter.isLogin()) {
            Intent intent = getIntent();
            if (intent != null && intent.getStringExtra("orderno") != null) {
                Intent intent_transmit = new Intent();
                intent_transmit.putExtra("page", "order");
                presentController(TabHomeActivity.class,intent_transmit);
            } else {
                presentController(TabHomeActivity.class);
            }
        } else {
            presentController(LoginActivity.class);
        }

        finish();
    }

    /**
     * 播放首次进入的视屏
     */
//    private void startVideo() {
//        String uri = "android.resource://" + getPackageName() + "/" + R.raw.welcome;
//
//        videoview.setVisibility(View.VISIBLE);
//
//        MediaController mediaco = new MediaController(this);
//        mediaco.setVisibility(View.INVISIBLE);
//        videoview.setVideoURI(Uri.parse(uri));
//        videoview.setMediaController(mediaco);
//        mediaco.setMediaPlayer(videoview);
//        videoview.requestFocus();
//        videoview.start();
//
//        videoview.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
//            @Override
//            public void onCompletion(MediaPlayer mp) {
//                videoview.postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        AppRoute.getInstance().nextController(MainActivity.this, TabHomeActivity.class, 0);
//                        finish();
//                    }
//                }, 0);
//            }
//        });
//    }
}
